package com.lestar.www.wuliancamtest.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lestar.www.wuliancamtest.R;
import com.lestar.www.wuliancamtest.WulianCam.WulianCamManager;

import rx.Subscriber;

/**
 * User: EEE
 * Date: 2016/8/3
 * Time: 11:33
 */
public class WifiSettingActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "WifiSettingActivity";
    private EditText deviceIdEt;
    private EditText wifiNameEt;
    private EditText wifiPwdEt;

    private WulianCamManager mCamManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_setting);

        deviceIdEt = (EditText)findViewById(R.id.device_id_et);
        assert deviceIdEt != null;

        wifiNameEt = (EditText)findViewById(R.id.wifi_name_et);
        assert wifiNameEt != null;

        wifiPwdEt = (EditText)findViewById(R.id.wifi_pwd_et);
        assert wifiPwdEt != null;

        Button bindCheckBtn = (Button) findViewById(R.id.bind_check_btn);
        assert bindCheckBtn != null;

        Button searchAndBindBtn = (Button) findViewById(R.id.search_and_bind_btn);
        assert searchAndBindBtn != null;

        Button qrWifiSettingBtn = (Button) findViewById(R.id.qr_wifi_setting_btn);
        assert qrWifiSettingBtn != null;

        Button checkWifiBtn = (Button) findViewById(R.id.check_wifi_btn);
        assert checkWifiBtn != null;

        mCamManager = WulianCamManager.getInstance(WifiSettingActivity.this);

        bindCheckBtn.setOnClickListener(this);
        checkWifiBtn.setOnClickListener(this);
        searchAndBindBtn.setOnClickListener(this);
        qrWifiSettingBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final String deviceId = deviceIdEt.getText().toString().trim();
        switch (v.getId()) {
            case R.id.bind_check_btn:
                assert deviceIdEt != null;
                mCamManager.bindCheck(deviceId).subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        // do nothing
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: ", e);
                        Toast.makeText(WifiSettingActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onNext(String s) {
                        Log.d(TAG, "onNext: bind seed: " + s);
                        Toast.makeText(WifiSettingActivity.this, "bind seed:" + s, Toast.LENGTH_LONG).show();
                    }
                });
                break;
            case R.id.check_wifi_btn:
                final String wifiName = mCamManager.getCurrentWifiSSID(WifiSettingActivity.this);
                assert wifiNameEt != null;
                wifiNameEt.setText(wifiName);
                break;
            case R.id.search_and_bind_btn:
                if (mCamManager.preAddDevice(
                        deviceIdEt.getText().toString().trim(),
                        wifiNameEt.getText().toString().trim(),
                        wifiPwdEt.getText().toString().trim())) {
                    mCamManager.addDeviceByMulticast(deviceId)
                            .subscribe(new Subscriber<String>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    Toast.makeText(WifiSettingActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                                }

                                @Override
                                public void onNext(String s) {
                                    Toast.makeText(WifiSettingActivity.this, s, Toast.LENGTH_LONG).show();
                                }
                            });
                }
                break;
            case R.id.qr_wifi_setting_btn:
                if (mCamManager.preAddDevice(
                        deviceIdEt.getText().toString().trim(),
                        wifiNameEt.getText().toString().trim(),
                        wifiPwdEt.getText().toString().trim())) {
                    startActivity(new Intent(WifiSettingActivity.this, BarcodeSettingActivity.class));
                } else {
                    Toast.makeText(WifiSettingActivity.this, "请填写配置信息", Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
    }
}
